<?php

namespace App\Http\Controllers\Admin;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    //后台登录页面
    public function getIndex()
    {

    	return view('Admin.login.index');	
    }
    public function  postDologin(Request $request)
    {
         //dd($request->input('vcode'));   
    	 //dd(session('Vcode')); 
    	//根据用户名查询
    	$user = DB::table('user')->where('username',$request->input('username'))->first();
    	//dd($user); 
    	if(empty($user)){
    		return back()->with('error','请输入用户名和密码');
    	}
        if($user->grade == 2){
            //判断验证码
             if(session('Vcode')!=$request->input('vcode')){
                     return back()->with('error','验证码不正确');
                }
        	//判断密码和库里是否一样
        	if(Hash::check($request->input('password'),$user->password)){
        		//登录成功
        		session(['id'=>$user->id]);
                //session(['name'=>$user->username]);

        		return redirect('/admin/')->with('success','登陆成功');
        	}else{
        		return back()->with('error','用户名或密码不正确');
        	}
        }else{
           return back()->with('error','权限不足');
        }
    }

    public function vcode()
    {
        ob_clean();//清除输出缓存区的内容 
        //生成验证码图片的Builder对象，配置相应属性 
        $builder = new CaptchaBuilder; 
        //可以设置图片宽高及字体 
        $builder->build($width = 100, $height = 40, $font = null); 
        //获取验证码的内容 
        $phrase = $builder->getPhrase(); 
        //把内容存入session 
        session(['Vcode'=>$phrase]); 
        //生成图片 
        header("Cache-Control: no-cache, must-revalidate"); 
        header('Content-Type: image/jpeg'); $builder->output(); 
        //看到图片输出 需要
       // die; //模板中使用 注意路由 

    	//判断密码和库里是否一样
    	if(Hash::check($request->input('password'),$user->password)){
            //登录成功
            session(['id'=>$user->id]);
            
            return redirect('/admin/') -> with('success');
        }else{
            return back() -> with('error','用户名或密码不正确');
        }
    }
           
}
